package team.nmsg.ge.system.service.impl;

import java.util.List;










import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import team.nmsg.ge.system.bean.ServerStatus;
import team.nmsg.ge.system.bean.PageObject;
import team.nmsg.ge.system.bean.SysBusinessLog;
import team.nmsg.ge.system.dao.INMSCommonDao;
import team.nmsg.ge.system.dao.ISystemLogDao;
import team.nmsg.ge.system.init.CommonDefine;
import team.nmsg.ge.system.service.ISystemLogService;
import team.nmsg.ge.system.util.SysDBHelper;



@Transactional
@Component
public class SystemLogServiceImpl implements ISystemLogService{
	
	private static final Log logger = LogFactory.getLog(SystemLogServiceImpl.class);
	
	@Autowired
	private ISystemLogDao systemLogDao = null;
	
	@Autowired
	private INMSCommonDao commonDao;

	@Override
	public PageObject<SysBusinessLog> queryLogs(int pageSize , int startIndex , SysBusinessLog businessLog,long startTime,long endTime) {
		PageObject<SysBusinessLog> page = new PageObject<SysBusinessLog>();
		long logCount = systemLogDao.querylogCount(businessLog,startTime,endTime);
		page.setSumCount( new Long(logCount).intValue() );
		
		List<SysBusinessLog> queryUserList = systemLogDao.QuerySysLogList(pageSize, startIndex, businessLog,startTime,endTime);
		
		page.setCurrentEntitys(queryUserList);
		
		return page;
	}
	
	@Override
	public boolean deleteLogById(String[] logIds) {
		
		try {
			for(int i=0;i<logIds.length;i++)
				systemLogDao.deleteLogById(logIds[i]);
		} catch (Exception e) {
			logger.error("" ,e );
			return false;
		}
		return true;
	}
	
	@Override
	public boolean isAuthPublish(){
		Session session = commonDao.getHSession();	
		Criteria criteria = session.createCriteria(ServerStatus.class);
		criteria.add(Restrictions.eq("skey",CommonDefine.PUBLISH_KEY));
		List existKey = criteria.list();
		if( existKey != null && existKey.size()>0 ){
			ServerStatus npsServerStatus = (ServerStatus)existKey.get(0);
			String value = npsServerStatus.getSvalue();
			if("true".equals(value))
				return true;
			else
				return false;
		}else
			return false;
	}
	
	@Override
	public boolean isAuthDownload(){
		Session session = commonDao.getHSession();	
		Criteria criteria = session.createCriteria(ServerStatus.class);
		criteria.add(Restrictions.eq("skey",CommonDefine.DOWNLOAD_KEY));
		List existKey = criteria.list();
		if( existKey != null && existKey.size()>0 ){
			ServerStatus npsServerStatus = (ServerStatus)existKey.get(0);
			String value = npsServerStatus.getSvalue();
			if("true".equals(value))
				return true;
			else
				return false;
		}else
			return false;
	}
	
	@Override
	public int setPublish_value(String status){
		String sql = "update npsserverstatus set svalue='" + status + "' where skey='" + CommonDefine.PUBLISH_KEY + "'";
		return commonDao.exeuteUpdate(sql);
	}

	@Override
	public int setDownload_key(String status) {
		String sql = "update npsserverstatus set svalue='" + status + "' where skey='" + CommonDefine.DOWNLOAD_KEY + "'";
		return commonDao.exeuteUpdate(sql);
	}
	
	
}
